package org.qiaice.controller;

import lombok.RequiredArgsConstructor;
import org.qiaice.entity.Dish;
import org.qiaice.entity.dto.DishDTO;
import org.qiaice.entity.dto.DishPageQueryDTO;
import org.qiaice.entity.vo.DishVO;
import org.qiaice.result.PageResult;
import org.qiaice.result.Result;
import org.qiaice.service.DishService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/admin/dish", produces = "application/json; charset=utf-8")
@RequiredArgsConstructor
public class DishController {

    private final DishService dishService;

    @GetMapping(value = "/{id}")
    public Result<DishVO> queryById(@PathVariable Long id) {
        return Result.ok(dishService.queryById(id));
    }

    @GetMapping(value = "/list")
    public Result<List<Dish>> queryByCategoryId(Long categoryId) {
        return Result.ok(dishService.lambdaQuery().eq(Dish::getCategoryId, categoryId).list());
    }

    @GetMapping(value = "/page")
    public Result<PageResult<DishVO>> page(DishPageQueryDTO dishPageQueryDTO) {
        return Result.ok(dishService.pageQuery(dishPageQueryDTO));
    }

    @PostMapping(value = "/status/{status}")
    public Result<Object> changeStatus(@PathVariable Integer status, Long id) {
        dishService.updateById(Dish.builder().status(status).id(id).build());
        return Result.ok(null);
    }

    @DeleteMapping
    public Result<Object> delete(@RequestParam List<Long> ids) {
        dishService.deleteByIds(ids);
        return Result.ok(null);
    }

    @PutMapping
    public Result<Object> change(@RequestBody DishDTO dishDTO) {
        dishService.change(dishDTO);
        return Result.ok(null);
    }

    @PostMapping
    public Result<Object> saveDish(@RequestBody DishDTO dishDTO) {
        dishService.saveDish(dishDTO);
        return Result.ok(null);
    }
}
